package org.example.commodity.activity.timertask;

import org.example.commodity.activity.entity.ActivityInfo;
import org.example.commodity.activity.timertask.processor.ICommodityActivityStartProcessor;
import io.netty.util.Timeout;
import io.netty.util.TimerTask;
import lombok.extern.slf4j.Slf4j;

/**
 * 商品活动执行时间轮执行任务
 */
@Slf4j
public abstract class BaseCommodityActivityStartTimerTask<T extends ActivityInfo> implements TimerTask {

    private final Long activityId;
    private final ICommodityActivityStartProcessor<T> startProcessor;

    public BaseCommodityActivityStartTimerTask(Long activityId,
                                               ICommodityActivityStartProcessor<T> onProcessor) {
        this.activityId = activityId;
        this.startProcessor = onProcessor;
    }

    @Override
    public void run(Timeout timeout) {
        log.info("商品活动{}执行任务开始执行", activityId);
        try {
            startProcessor.onActivity(activityId);
        }catch (Exception e){
            log.error("商品活动{}执行失败，{}", activityId,e.getMessage());
        }
        log.error("商品活动{}执行成功", activityId);
    }
}
